﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.Web;

namespace Kiosk.Utilities
{
    public class RssReader
    {
        public List<RssEntry> entries = new List<RssEntry>();
        public int currentEntry = 0;
        private string url = "";
        public string Title { get; set; }

        private RssReader()
        {
        
        }

        public RssReader(FeedElement f)
        {
            this.Title = f.Title;
            this.url = f.Url;
            Refresh();
        }




        /// <summary>
        /// Gets the next entry in this Rss feed.
        /// </summary>
        /// <returns>The next RSS entry for this feed</returns>
        public  RssEntry GetNextEntry()
        {
            RssEntry e = new RssEntry();
            if (entries.Count > 0)
            {
                if (currentEntry == entries.Count)
                    currentEntry = 0;
                e = entries[currentEntry];
                currentEntry++;
            }
            else
            {
                e.Headline = "No RSS Entries";
                e.Description = "There were no RSS entries for this feed, please the feed URL or your network connection";

            }
            return e;
        }

        /// <summary>
        /// Retrieves the newest entries for this RSS feed
        /// </summary>
        public void Refresh()
        {   
            try
            {
                entries.Clear();

                
                XDocument rssDoc = XDocument.Load(url);
                string namesp = "";
                if (rssDoc.Root.Attribute("xmlns") != null)
                {
                    namesp = rssDoc.Root.Attribute("xmlns").Value;
                }
                XNamespace ns = namesp;
                var posts = from item in rssDoc.Descendants(ns + "item")
                            select new RssEntry
                            {
                                FeedTitle = "Latest from " + this.Title,
                                Headline = item.Element(ns +  "title").Value,
                                Description = Regex.Replace(item.Element(ns +  "description").Value, @"<(.|\n)*?>", "")
                            };
                entries.AddRange(posts);
            }
            catch (Exception ex)
            { // eat it
            }
        }


    }

    public class RssEntry
    {
        private string headline;
        public string Headline {
            get
            {
                return headline;
            }
            set
            { headline = HttpUtility.HtmlDecode(value); }
        }
        private string desc;
        public string Description { 
         get
            { return desc; }
            set
            { desc = HttpUtility.HtmlDecode(value); }
        }
        public string FeedTitle { get; set; } 
    }
}
